package com.example.myblog.controller.admin;
import com.example.myblog.entity.Columnist;
import com.example.myblog.service.TypeService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;

@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    public String types(Model model,Integer page){
        page = page==null ? 1 : page;
        Page<Columnist> result = typeService.listType(page);
        model.addAttribute("page",result);
        return "admin/types";
    }


    @GetMapping("/types/input")
    public String input(Model model){
        model.addAttribute("columnist",new Columnist());
        return "admin/types-input";
    }

    //增
    @PostMapping("/types")
    public ModelAndView post(Columnist columnist,BindingResult result){
        ModelAndView mv = new ModelAndView();
        Columnist c1 = typeService.getTypeByName(columnist.getColName());
        //分类名称已存在，定义错误信息
        if (c1!=null) {
            result.rejectValue("colName", "colNameError", "分类名称重复");
        }
        if (result.hasErrors()){
            mv.setViewName("admin/types-input");
            return mv;
        }
        columnist.setCreateTime(new Date());
        columnist.setBlogCount(0);
        int a = typeService.addType(columnist);
        String msg = a>0?"操作成功！":"操作失败！";
        mv.addObject("message",msg);
        mv.setViewName("redirect:/admin/types");
        return mv;
    }
    //删
    @GetMapping("/types/{id}/delete")
    public ModelAndView delete(@PathVariable int id){
        ModelAndView mv = new ModelAndView();
        int a = typeService.deleteById(id);
        String msg = a>0?"操作成功！":"操作失败！";
        mv.addObject("message",msg);
        mv.setViewName("redirect:/admin/types");
        return mv;
    }

    //改
    @GetMapping("/types/{id}/input")
    public ModelAndView update(@PathVariable int id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/types-update");
        mv.addObject("columnist",typeService.findById(id));
        return mv;
    }
    @PostMapping("/types/update")
    public ModelAndView update(Columnist columnist, BindingResult result){
        ModelAndView mv = new ModelAndView();
        Columnist c1 = typeService.getTypeByName(columnist.getColName());
        if (c1!=null) {
            result.rejectValue("colName", "colNameError", "分类名称重复");
        }
        if (result.hasErrors()){
            mv.setViewName("admin/types-update");
            return mv;
        }
        columnist.setCreateTime(new Date());
        int a = typeService.update(columnist);
        String msg = a>0?"操作成功！":"操作失败！";
        mv.addObject("message",msg);
        mv.setViewName("redirect:/admin/types");
        return mv;
    }

}
